package com.server.service;

import java.util.List;



import com.client.service.UserManagementService;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import com.server.store.PoolManager;
import com.server.store.context.UserManager;
import com.server.store.data.AssureManager;
import com.shared.context.User;
import com.shared.data.TypeIData;
import com.shared.model.Assure;
/**
 * Implémentation du service de gestion des {@link User}
 * @author queyroi
 *
 */
@SuppressWarnings("serial")
public class UserManagementServiceImpl extends RemoteServiceServlet implements UserManagementService{
        
        public User delete(User user) throws IllegalArgumentException {         
                checkCurrentIsAdministrator();
                checkInput(user);
                ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).delete(user);
                return user;
                
        }

        public List getList() throws IllegalArgumentException {         
                checkCurrentIsAdministrator();
                return ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).getList();
        }

        public User save(User user,Assure assure) throws IllegalArgumentException {
                checkInput(user);
                checkInput(assure);

                user.setAdministrator(false);

                ((AssureManager) PoolManager.get().getDataManager(TypeIData.ASSURE)).save(assure);
                user.setAssure(assure);
                ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).save(user);             
                return user;
        }

        public User update(User user) throws IllegalArgumentException {
                checkInput(user);               
                ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).update(user);
                return user;
        }
        
        public void checkInput(Object obj) throws IllegalArgumentException{
                if(obj==null) throw new IllegalArgumentException("Echec d'enregistrement : vérifier les paramètres");
        }
        
        public void checkCurrentIsAdministrator() throws IllegalArgumentException{
                if(!((User) getThreadLocalRequest().getSession().getAttribute("user")).isAdministrator()) throw new IllegalArgumentException("Seul un administrateur est autorisé à réalisé cette opération");
        }

        
        public User saveForAdmin(User user) throws IllegalArgumentException {
                checkInput(user);
                checkCurrentIsAdministrator();
                ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).save(user);
                return user;
        }

        public User getUserCourant() {
                User u = (User) getThreadLocalRequest().getSession().getAttribute("user");
                return u;
        }
        
        public Assure getAssureCourant() throws IllegalArgumentException {
                try {
                        Assure assure = ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).getAssure(getUserCourant());                    
                        return assure;
                } catch (EntityNotFoundException e) {                   
                        throw new IllegalArgumentException("Echec de la requete");
                }
        }

        public String changePassword(String oldP, String newP)
                        throws IllegalArgumentException {
                
                if(oldP.equals(getUserCourant().getPassword())){
                        getUserCourant().setPassword(newP);
                        ((UserManager) PoolManager.get().getDataManager(TypeIData.USER)).update(getUserCourant());
                }                                       
                throw new IllegalArgumentException("Echec de la requete : Utilisateur non identifié");
        }
        

}